package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import com.zjitc.utils.MailUtils;
import java.util.UUID;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/4/17 17:05
 * @description:
 */
@Controller
@RequestMapping("/user_main")
public class UserController {

  @Autowired
  private UserService userService;

  /**
   * 登陆
   * @param username
   * @param password
   * @param session
   * @return
   */
  @RequestMapping(value = "/login")
  public String login(
      //required代表不需要传参数进去
      @RequestParam(value = "username", required = false) String username,
      @RequestParam(value = "password", required = false) String password,
      HttpSession session
  ) {
    //判断username和password如果都是空，则进入登陆界面
    if (username == null && password == null) {
      return "login";
    }
    ServerResponse serverResponse = userService.login(username, password);
    if (serverResponse.Success()) {
      session.setAttribute("user", serverResponse.getData());
      return "main";
    }
    session.setAttribute("msgs",serverResponse.getMsg());
    return "login";
  }

  /**
   * 进入忘记密码的页面
   * @param session
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/forgetPassword")
  public String forgetPassword(HttpSession session) throws Exception {
    return "forget";
  }

  /**
   * 进入修改密码的页面
   * @param username
   * @param session
   * @return
   * @throws Exception
   */
  @RequestMapping(value = "/forgetUser")
  public String forgetUser(String username,HttpSession session) throws Exception {
    UUID uuid =UUID.randomUUID();
    ServerResponse<User> serverResponse = userService.forgetPassword(username);
    ServerResponse serverResponse1 = userService.findUUID(username, String.valueOf(uuid));
    User user = userService.findUserByUsername(username);
    session.setAttribute("username",username);
    MailUtils.sendMail(username,user.getPhone(),user.getEmail(),uuid);
    return "forgetUUID";
  }

  /**
   * 用UUID修改密码
   * @param password
   * @return
   */
  @RequestMapping(value = "/amendPassword")
  public String amendPassword(String password,String uuid){
    ServerResponse serverResponse = userService.amendPassword(password,uuid);
    return "login";
  }

  /**
   * 注销
   * @param session
   * @return
   */
  @RequestMapping(value = "/logout")
  public String logout(HttpSession session){
    session.removeAttribute("user");
    return "main";
  }

  /**
   * 注册
   * @param user
   * @return
   */
  @RequestMapping(value = "/register")
  public String register(User user) {
    if (user.getUsername() == null) {
      return "register";
    }
    ServerResponse serverResponse = userService.register(user);
    if (serverResponse.Success()) {
      serverResponse.getData();
    }
    return "login";
  }

  /**
   * 修改信息，回显功能
   * @param session
   * @return
   */
  @RequestMapping("/echoInfo")
  public String updateInfo(HttpSession session){
    User user = (User) session.getAttribute("user");
    ServerResponse response = userService.echoInfo(user.getUsername());
    if (user == null){
        return "login";
    }
    return "amend_info";
  }

  /**
   * 修改
   * @param user
   * @param session
   * @return
   */
  @RequestMapping("/updateInfo")
  public String updateInfo(User user, HttpSession session){
    session.setAttribute("user",user);
      ServerResponse<User> response = userService.updateInfo(user);
      if (user == null) {
          return "login";
      }
      if (response.getStatus() == 1) {
          session.setAttribute("", "更新错误");
          return "amend_info";
      } else {
          session.removeAttribute("");
          return "main";
      }
  }

  /**
   * 主页面
   * @return
   */
  @RequestMapping(value = "/main")
  public String main() {
    return "main";
  }
}
